package com.jhclothing.outbound.service.impl;

import com.jhclothing.common.utils.DateUtils;
import com.jhclothing.outbound.domain.FabricOutbound;
import com.jhclothing.outbound.mapper.FabricOutboundMapper;
import com.jhclothing.outbound.service.IFabricOutboundService;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.service.IFabricService;
import com.jhclothing.product.vo.StatisticInOutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 布料出库Service业务层处理
 *
 * @author zyp
 * @date 2024-04-25
 */
@Service
public class FabricOutboundServiceImpl implements IFabricOutboundService {
    @Autowired
    private FabricOutboundMapper fabricOutboundMapper;
    @Autowired
    private IFabricService fabricService;

    /**
     * 查询布料出库
     *
     * @param outboundId 布料出库主键
     * @return 布料出库
     */
    @Override
    public FabricOutbound selectFabricOutboundByOutboundId(Long outboundId) {
        return fabricOutboundMapper.selectFabricOutboundByOutboundId(outboundId);
    }

    /**
     * 查询布料出库列表
     *
     * @param fabricOutbound 布料出库
     * @return 布料出库
     */
    @Override
    public List<FabricOutbound> selectFabricOutboundList(FabricOutbound fabricOutbound) {
        return fabricOutboundMapper.selectFabricOutboundList(fabricOutbound);
    }

    /**
     * 统计一定时间内的布料出库数量
     */
    public List<StatisticInOutVo> statisticFabricOutboundNum(StatisticInOutVo statisticInOutVo) {
        return fabricOutboundMapper.statisticFabricOutboundNum(statisticInOutVo);
    }

    /**
     * 新增布料出库
     *
     * @param fabricOutbound  布料出库
     * @param fabricInventory 更新后的布料数量
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFabricOutbound(FabricOutbound fabricOutbound, BigDecimal fabricInventory) {
        fabricService.updateFabricInventory(fabricOutbound.getFabricId(), fabricInventory);
        fabricOutbound.setCreateTime(DateUtils.getNowDate());
        return fabricOutboundMapper.insertFabricOutbound(fabricOutbound);
    }

    /**
     * 修改布料出库
     *
     * @param fabricOutbound 布料出库
     * @return 结果
     */
    @Override
    @Transactional
    public String updateFabricOutbound(FabricOutbound fabricOutbound) {
        // 上次出库信息
        FabricOutbound originOutbound = this.selectFabricOutboundByOutboundId(fabricOutbound.getOutboundId());
        BigDecimal originOutboundNum = originOutbound.getOutboundNum();

        // 当前出库布料数量
        Fabric fabric = fabricService.selectFabricByFabricId(fabricOutbound.getFabricId());
        BigDecimal nowInventory = fabric.getNowInventory();
        String fabricCodeAndName = fabric.getFabricCodeAndName();

        // 本次出库布料类别与之前记录一致
        if (fabricOutbound.getFabricId() == originOutbound.getFabricId()) {
            BigDecimal fabricInventory = originOutboundNum.add(nowInventory);
            if (fabricInventory.compareTo(fabricOutbound.getOutboundNum()) < 0) {
                return String.format("布料 %s 库存不足，请重新选择出库数量", fabricCodeAndName);
            }
            fabricInventory = fabricInventory.subtract(fabricOutbound.getOutboundNum());
            fabricService.updateFabricInventory(fabricOutbound.getFabricId(), fabricInventory);
        } else { // 本次出库布料类别与之前记录不一致
            if (nowInventory.compareTo(fabricOutbound.getOutboundNum()) < 0) {
                return String.format("布料 %s 库存不足，请重新选择出库数量", fabricCodeAndName);
            }
            Fabric originFabric = fabricService.selectFabricByFabricId(originOutbound.getFabricId());
            fabricService.updateFabricInventory(originFabric.getFabricId(), originOutboundNum.add(originFabric.getNowInventory()));
            fabricService.updateFabricInventory(fabricOutbound.getFabricId(), fabric.getNowInventory().subtract(fabricOutbound.getOutboundNum()));
        }
        fabricOutbound.setUpdateTime(DateUtils.getNowDate());
        fabricOutboundMapper.updateFabricOutbound(fabricOutbound);
        return "SUCCESS";
    }

    /**
     * 批量删除布料出库
     *
     * @param outboundIds 需要删除的布料出库主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFabricOutboundByOutboundIds(Long[] outboundIds) {
        for (Long outboundId : outboundIds) {
            FabricOutbound fabricOutbound = this.selectFabricOutboundByOutboundId(outboundId);
            Fabric fabric = fabricService.selectFabricByFabricId(fabricOutbound.getFabricId());
            BigDecimal fabricInventory = fabric.getNowInventory().add(fabricOutbound.getOutboundNum());
            fabricService.updateFabricInventory(fabricOutbound.getFabricId(), fabricInventory);
        }
        return fabricOutboundMapper.deleteFabricOutboundByOutboundIds(outboundIds);
    }

    /**
     * 删除布料出库信息
     *
     * @param outboundId 布料出库主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFabricOutboundByOutboundId(Long outboundId) {
        FabricOutbound fabricOutbound = this.selectFabricOutboundByOutboundId(outboundId);
        Fabric fabric = fabricService.selectFabricByFabricId(fabricOutbound.getFabricId());
        BigDecimal fabricInventory = fabric.getNowInventory().add(fabricOutbound.getOutboundNum());
        fabricService.updateFabricInventory(fabricOutbound.getFabricId(), fabricInventory);
        return fabricOutboundMapper.deleteFabricOutboundByOutboundId(outboundId);
    }
}
